from rest_framework.viewsets import ModelViewSet
from goods.models import SPU,Brand,GoodsCategory,GoodsChannel, GoodsChannelGroup
from meiduo_admin.my_paginate import MyPageNumberPagination
from . import spu_serializers
from rest_framework.generics import ListAPIView
from rest_framework.response import Response
from rest_framework.views import APIView
from fdfs_client.client import Fdfs_client
from django.conf import settings
from meiduo_admin.good.ChannelsSerializers import  ChannelsSerializer, ChannelsGroupSerializer, CategoriesSerializer
from meiduo_admin.good.BrandsSerializers import  BrandsSerializer


#1,spu信息
class SPUViewSet(ModelViewSet):
    pagination_class = MyPageNumberPagination
    serializer_class = spu_serializers.SPUViewSetSerializer
    queryset = SPU.objects.all()

#2,spu,品牌信息
class SPUBrandSimpleView(ListAPIView):
    serializer_class = spu_serializers.SPUBrandSimpleSerializer
    queryset = Brand.objects.all()

#3,spu, 一级分类
class SPUCategoryView(ListAPIView):
    serializer_class = spu_serializers.SPUCategorySerializer
    queryset = GoodsCategory.objects.filter(parent=None).all()

#4, spu,二级,三级分类
class SPUCategorySubView(ListAPIView):
    serializer_class = spu_serializers.SPUCategorySerializer

    def get_queryset(self):
        #1,获取分类编号
        category_id = self.kwargs.get("category_id")

        #2,获取子分类
        return GoodsCategory.objects.get(id=category_id).subs.all()

#5, 图片上传
class SPUImageUploadView(APIView):
    def post(self,request):
        #1,获取参数
        image = request.FILES.get("image")

        #2,校验参数
        if not image:
            return Response(status=400)

        #3,入库(fdfs)
        client = Fdfs_client(settings.FDFS_CONFIG)
        ret = client.upload_by_buffer(image.read())

        if ret["Status"] != "Upload successed.":
            return Response(status=400)

        #4,返回响应
        return Response({
            "img_url": settings.BASE_URL + ret.get("Remote file_id")
        })

# 频道管理
class ChannelsViewSet(ModelViewSet):
    serializer_class = ChannelsSerializer
    queryset = GoodsChannel.objects.all()
    pagination_class = MyPageNumberPagination


    def retrieve(self, request, *args, **kwargs):
        instance = self.get_object()
        serializer = self.get_serializer(instance)

        return Response(serializer.data)


class ChannelTypesView(ListAPIView):
    serializer_class = ChannelsGroupSerializer
    queryset = GoodsChannelGroup.objects.all()

class CategoriesView(ListAPIView):
    serializer_class = CategoriesSerializer
    queryset = GoodsCategory.objects.filter(parent=None).all()

# 品牌管理
class BrandsViewSet(ModelViewSet):
    queryset = Brand.objects.all()
    serializer_class = BrandsSerializer
    pagination_class = MyPageNumberPagination

    def create(self, request, *args, **kwargs):
        # 1,获取参数
        image = request.FILES.get("logo")
        name = request.data.get("name")
        first_letter = request.data.get("first_letter")

        # 2,校验参数
        if not all([image, name,first_letter]):
            return Response(status=403)

        # 3,数据入库
        client = Fdfs_client(settings.FDFS_CONFIG)
        ret = client.upload_by_buffer(image.read())

        # 3,1判断是否成功
        if ret["Status"] != "Upload successed.":
            return Response(status=400)

        # 3,2数据入库图片
        image_url = ret.get("Remote file_id")
        Brand.objects.create(name=name, logo=image_url, first_letter=first_letter)


        # 4,返回响应
        return Response(status=201)